package com.manelrodriguez.applications.ibex35.adapters;

import java.text.NumberFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.manelrodriguez.applications.ibex35.R;
import com.manelrodriguez.applications.ibex35.to.Movimiento;

public class CarteraMovListAdapter extends ArrayAdapter<Movimiento> {
	private int layoutResourceId;
	TextView tv_1;
	TextView tv_2;
	TextView tv_3;
	TextView tv_4;
	NumberFormat numberFormat;
	
	public CarteraMovListAdapter(Context context, int layoutResourceId, List<Movimiento> items){
		super(context, layoutResourceId, items);
        this.layoutResourceId = layoutResourceId;
        numberFormat = NumberFormat.getInstance();
		numberFormat.setMaximumFractionDigits(2);
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout itemView;
		Movimiento mov = getItem(position);
		if(convertView==null)
		{
			itemView = new RelativeLayout(getContext());
			LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			vi.inflate(layoutResourceId, itemView, true);
		}else{
			itemView = (RelativeLayout)convertView;
		}
		tv_1 = (TextView)itemView.findViewById(R.id.cartera_id_empresa_mov);
		tv_2 = (TextView)itemView.findViewById(R.id.cartera_num_acciones_mov);
		tv_3 = (TextView)itemView.findViewById(R.id.cartera_importe_mov);
		tv_4 = (TextView)itemView.findViewById(R.id.cartera_comision_mov);
		tv_1.setText(mov.getIdEmpresa());
		tv_2.setText(String.valueOf(numberFormat.format(mov.getNumAcciones())));
		tv_3.setText(String.valueOf(numberFormat.format(mov.getPrecioUnitario())));
		tv_4.setText(String.valueOf(numberFormat.format(mov.getComision())));
		return itemView;
	}
}
